import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_form.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/location_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/index.dart';
import 'package:enjoy_plus/pages/notice/index.dart';
import 'package:enjoy_plus/pages/profile/index.dart';
import 'package:enjoy_plus/pages/tab_bar_page.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => const NoticeDetail(),
        '/login': (context) => const LoginPage(),
        // 写了就不会拦截了
        // '/profile': (context) => const ProfilePage(),
      },
      initialRoute: '/', // 默认打开的页面
      // 动态生成路由，运行时根据路由名称或其他条件动态地创建和返回路由
      onGenerateRoute: (settings) {
        // 获取 token
        final token = TokenManager().getToken();
        // 判断 token 是否为空，如果为空则跳转到登录页面
        if (token == null && settings.name != '/login') {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        }
        // 已登录，判断是否为个人中心页
        if (settings.name == '/profile') {
          return MaterialPageRoute(
              builder: (context) =>
                  ProfilePage(userInfo: settings.arguments as Map));
        }
        if (settings.name == '/house_list') {
          return MaterialPageRoute(builder: (context) => const HouseList());
        }
        // 分步表单1: 选择小区
        if (settings.name == '/location_list') {
          return MaterialPageRoute(builder: (context) => const LocationList());
        }
        // 分步表单2: 选择楼栋
        if (settings.name == '/building_list') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => BuildingList(
              point: params['point'], // 传递小区信息
            ),
          );
        }
        // 分步表单3: 选择房间
        if (settings.name == '/room_list') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => RoomList(
              point: params['point'], // 传递小区信息
              building: params['building'], // 传递楼栋信息
            ),
          );
        }

        if (settings.name == '/house_form') {
          Map params = settings.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => HouseForm(
              params: params,
            ),
          );
        }
        // 其他情况返回 null，由默认路由处理
        return null;
      },
    ),
  );
}
